<template>
  <div class="item-one">
    
    <div class="Box">
        <!-- 商戶基本信息 -->
      <div class="one" >
        <div class="title">
          <span>商戶基本信息</span><span v-if="merchantinfo.status == 9" class="err">驳回原因：{{merchantinfo.reviewedReason}}</span>
          <div>
            <el-button icon="el-icon-refresh"></el-button>
          </div>
        </div>
        <div class="basicBox">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="160px" class="demo-ruleForm">
                <el-form-item v-if="userinfo.rolesId !=2 && userinfo.rolesId !=3 && userinfo.rolesId !=4" label="一级服务商:" prop="agent1Id">
                    <el-autocomplete
                        class="service"
                        v-model="ruleForm.agent1"
                        :fetch-suggestions="querySearch"
                        placeholder="请选择一级服务商"
                        @select="serviceid"
                    ></el-autocomplete>
                </el-form-item>
                <el-form-item v-if="userinfo.rolesId !=3 && userinfo.rolesId !=4" label="二级服务商:" prop="agent2Id">
                    <el-autocomplete
                        class="service"
                        v-model="ruleForm.agent2"
                        :fetch-suggestions="querySearch2"
                        placeholder="请选择二级服务商"
                        @select="serviceid2"
                    ></el-autocomplete>
                </el-form-item>
                <el-form-item v-if="userinfo.rolesId !=4" label="业态" prop="bdId">
                    <!-- <el-input placeholder="业态" v-model="ruleForm.bdId"></el-input> -->
                    <el-autocomplete
                        class="service"
                        v-model="ruleForm.BDname"
                        :fetch-suggestions="BDname"
                        placeholder="业态"
                        @select="BDid"
                    ></el-autocomplete>
                </el-form-item>
                <el-form-item label="商户名称" prop="merchantName"><el-input placeholder="商户名称" v-model="ruleForm.merchantName"></el-input></el-form-item>
                <el-form-item label="商户简称" prop="merchantShortName"><el-input placeholder="商户简称" v-model="ruleForm.merchantShortName"></el-input></el-form-item>
                <!-- <el-form-item label="注册名称" prop="merchantBusinessName"><el-input placeholder="注册名称" v-model="ruleForm.merchantBusinessName"></el-input></el-form-item> -->
                <el-form-item label="商户联系人" prop="contactName"><el-input placeholder="商户联系人" v-model="ruleForm.contactName"></el-input></el-form-item>
                <el-form-item label="负责人电话" prop="contactPhone"><el-input placeholder="负责人电话" v-model="ruleForm.contactPhone"></el-input></el-form-item>
                <!-- <el-form-item label="负责人邮箱" prop="contactEmail"><el-input placeholder="负责人邮箱" v-model="ruleForm.contactEmail"></el-input></el-form-item>
                <el-form-item label="所属行业" prop="industry1">
                    <el-select class="service" v-model="ruleForm.merchantMcc1" placeholder="所属行业" @change="wx2fun">
                        <el-option
                        v-for="(value, index) in wx1arr"
                        :key="index"
                        :label="value.firstname"
                        :value="value.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                 <el-form-item prop="industry2">
                    <el-select class="width100" v-model="ruleForm.merchantMcc2" placeholder="细分行业" @change="wx3fun">
                        <el-option
                        v-for="(value, index) in wx2arr"
                        :key="index"
                        :value="value.id"
                        :label="value.secondname"
                        ></el-option>
                    </el-select>
                </el-form-item>
                 <el-form-item prop="industry3">
                     <el-select class="width100" v-model="ruleForm.merchantMccCode" placeholder="三级行业" @change="wx4fun">
                        <el-option
                        v-for="(value, index) in wx3arr"
                        :key="index"
                        :value="value.code"
                        :label="value.thirdname"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="客服电话(必须手机号)" prop="servicePhone"><el-input placeholder="客服电话" v-model="ruleForm.servicePhone"></el-input></el-form-item>
                <el-form-item label="所在省" prop="province">
                    <el-select class="width100" v-model="ruleForm.province" placeholder="所在省" @change="city">
                        <el-option
                        v-for="(item, index) in provincearr"
                        :key="index"
                        :label="item.name"
                        :value="item.code"
                        :codeid="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所在市" prop="city">
                    <el-select class="width100" v-model="ruleForm.city" placeholder="所在市" @change='county'>
                        <el-option
                        v-for="(value, index) in cityarr"
                        :key="index"
                        :label="value.name"
                        :value="value.code"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所在区" prop="county">
                    <el-select class="width100" v-model="ruleForm.county" placeholder="所在区" @change='countyCode'>
                        <el-option
                        v-for="(value, index) in countyarr"
                        :key="index"
                        :label="value.name"
                        :value="value.code"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商户详细地址" prop="merchantAddress"><el-input placeholder="商户详细地址" v-model="ruleForm.merchantAddress"></el-input></el-form-item> -->
                <!-- <el-form-item><el-button type="primary" @click="basic('ruleForm')">下一步</el-button></el-form-item> -->
            </el-form>
        </div>
      </div>





    <!-- 商戶费率及证照 -->
      <div class="three">
        
      
        <div class="btnBox">
            <!-- <el-button type="primary" @click="gotwo()">上一步</el-button> -->
            <el-button type="primary" @click="addmerchant()">提交</el-button>
        </div>
    </div>
    </div>
  </div>
</template>

<script>
import {
    getservice,
    getBD,
    wx1,
    wx2,
    wx3,
    province,
    city,
    county,
    parentlist,
    banklist,
    ratelist,
    merchantsave,
    uploadfileget,
    updateToReview,
    merchantupdate
} from "@/api/api";
import bus from '../common/bus';
export default {
//   name: "baseform",
  data: function() {
    return {
    token: { token: sessionStorage.getItem("token") },
    tags:[],
    ones: true,
    twos: false,
    threes: false,
    geti:false,
    loading:'',
    merchantinfo:"",
    uploadfile:"",
    upload: this.$store.state.urlexcel +"/product/v31/api/merchant/uploadfile/upload",
    
    ratelists:[],
    servicearr1:[],
    servicearr2:[],
    BDarr:[],
    wx1arr: [],
    wx1code: "",
    wx2arr: [],
    wx3arr:[],
    provincearr:[],
    cityarr:[],
    citysarr:[],
    countyarr:[],
    bankarr:[],
    userinfo:{},
    parentlists:[],
    ruleForm: {
        agent1Id: '', // 一级服务商
        agent2Id: '', //二级服务商
        bdId: '', // DB
        merchant_name: '',//商户名称
        instNo:'60000001'
    },
    settlement:{
        licenceType:'',
        photoType: '1'
    },
    settlement1:{
        photoType: '1'
    },
    picture:{
        pBankcardA:'',
        pBankcardB:'',
        pIdcardA:'',
        pIdcardB:'',
        pOutside:'',
        pInside:'',
        pIdcardHander:'',
        pBankIdcardA:'',
        pBankIdcardB:'',
        pOther:'',
        pLicense:'',
        pOpenAccount:'',
        pUnincorporated:'',
        otherPlatform:'',
        aliMaterials:''
    },
    
    rules: {
        agent1Id: [{required: true, message: '请选择一级服务商'}],
        agent2Id: [{required: true, message: '请选择二级服务商'}],
        bdId: [{required: true, message: '请选择业态'}],
        merchantName: [{required: true, message: '请输入商户名称', trigger: 'blur'}],
        merchantShortName:[{required: true, message: '请输入商户简称', trigger: 'blur'}],
        merchantBusinessName:[{required: true, message: '请输入注册名称', trigger: 'blur'}],
        contactName:[{required: true, message: '请输入商户联系人', trigger: 'blur'}],
        contactPhone:[{pattern: /^1[34578]\d{9}$/,required: true, message: '请输入正确电话', trigger: 'blur'}],
        contactEmail:[{type: 'email', required: true, message: '请输入正确邮箱', trigger: 'blur'}],
        industry1:[{required: true, message: '请选择所属行业', trigger: 'blur'}],
        industry2:[{required: true, message: '请选择二级所属行业', trigger: 'blur'}],
        industry3:[{required: true, message: '请选择三级所属行业', trigger: 'blur'}],
        servicePhone:[{pattern: /^1[34578]\d{9}$/,required: true, message: '请输入正确电话', trigger: 'blur'}],
        province:[{required: true, message: '请选择省', trigger: 'blur'}],
        city:[{required: true, message: '请选择市', trigger: 'blur'}],
        county:[{required: true, message: '请选择区', trigger: 'blur'}],
        merchantAddress:[{required: true, message: '请输入详细地址', trigger: 'blur'}],
    },
    // 企业
    settlements:{
        merchant_type: [{required: true, message: '请选择商户类型', trigger: 'blur'}],
        businessLicence: [{required: true, message: '营业执照号', trigger: 'blur'}],
        businessLicenceExpire:[{required: true, message: '营业有效期', trigger: 'blur'}],
        accountType:[{required: true, message: '结算账户类型', trigger: 'blur'}],
        principalCertNo:[{required: true, message: '请输入结算人证件号码', trigger: 'blur'}],
        principalCertExpire:[{required: true, message: '请输入结算人证件有效期', trigger: 'blur'}],
        accountCardName:[{required: true, message: '请输入结算账户开户名', trigger: 'blur'}],
        accountCardNum:[{required: true, message: '请输入结算账户开户号', trigger: 'blur'}],
        accountPhone:[{required: true, message: '请输入银行预留手机号', trigger: 'blur'}],
        sheng:[{required: true, message: '请选择开户行', trigger: 'blur'}],
        province:[{required: true, message: '请选择所在省', trigger: 'blur'}],
        city:[{required: true, message: '请选择所在市', trigger: 'blur'}],
        accountBankName:[{required: true, message: '请选择结算户开户支行'}],
    },
    // 小微企业
    settlements1:{
        principalCertNo:[{required: true, message: '请输入结算人证件号码', trigger: 'blur'}],
        principalCertExpire:[{required: true, message: '请输入结算人证件有效期', trigger: 'blur'}],
        accountCardName:[{required: true, message: '请输入结算账户开户名', trigger: 'blur'}],
        accountCardNum:[{required: true, message: '请输入结算账户开户号', trigger: 'blur'}],
        accountPhone:[{required: true, message: '请输入银行预留手机号', trigger: 'blur'}],
        sheng:[{required: true, message: '请选择开户行', trigger: 'blur'}],
        province:[{required: true, message: '请选择所在省', trigger: 'blur'}],
        city:[{required: true, message: '请选择所在市', trigger: 'blur'}],
        accountBankName:[{required: true, message: '请选择结算户开户支行'}],
    },
    pictures:{

    }
      
    };
  },
  created(){
    
    this.userinfo = JSON.parse(sessionStorage.getItem("userInfo"));
    this.merchantinfo = JSON.parse(sessionStorage.getItem('auditinfo'))
    this.tags = JSON.parse(sessionStorage.getItem('tags'))
    this.getservice()
    this.wx1();
    this.province();
    this.parentlist();
    // this.uploadfileget();
    if(this.userinfo.rolesId == 2){
        this.serviceid(this.userinfo.fid)

    }else if(this.userinfo.rolesId == 3){
        this.ruleForm.agent2Id = this.userinfo.fid
        this.getBD(this.userinfo.fid)
    }else if(this.userinfo.rolesId == 4){
        this.ruleForm.bdId = this.userinfo.fid
    }
    // 获取费率
     ratelist({
         bdId: this.merchantinfo.bdId
     }).then(res => {
            if (res.code == 405) {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
            } else {
            this.ratelists = res.data;
            }
        });
    // 获取行业
    this.wx2fun(this.merchantinfo.merchantMcc1)
    this.wx3fun(this.merchantinfo.merchantMcc2)
    // 回显信息
    // 商户基本信息
    this.ruleForm = {
        agent1: this.merchantinfo.agent1Name,
        agent1Id: this.merchantinfo.agent1Id,
        agent2: this.merchantinfo.agent2Name,
        agent2Id: this.merchantinfo.agent2Id,
        BDname: this.merchantinfo.bdName,
        bdId: this.merchantinfo.bdId,
        merchantName: this.merchantinfo.merchantName,
        merchantShortName: this.merchantinfo.merchantShortName,
        merchantBusinessName: this.merchantinfo.merchantBusinessName,
        contactName: this.merchantinfo.contactName,
        contactPhone: this.merchantinfo.contactPhone,
        contactEmail: this.merchantinfo.contactEmail,
        merchantMcc1Name: this.merchantinfo.merchantMcc1Name,
        merchantMcc1: this.merchantinfo.merchantMcc1,
        merchantMcc2Name: this.merchantinfo.merchantMcc2Name,
        merchantMcc2: this.merchantinfo.merchantMcc2,
        merchantMccName:this.merchantinfo.merchantMccName,
        merchantMccCode:this.merchantinfo.merchantMccCode,
        merchantMcc3:this.merchantinfo.merchantMcc3,
        servicePhone:this.merchantinfo.servicePhone,
        province: this.merchantinfo.merchantProvince,
        provinceCode: this.merchantinfo.provinceCode,
        merchantProvince:this.merchantinfo.merchantProvince,
        provinceCode: this.merchantinfo.provinceCode,
        city: this.merchantinfo.merchantCity,
        merchantCity: this.merchantinfo.merchantCity,
        cityCode: this.merchantinfo.cityCode,
        county: this.merchantinfo.merchantCounty,
        merchantCounty: this.merchantinfo.merchantCounty,
        countyCode: this.merchantinfo.countyCode,
        merchantAddress: this.merchantinfo.merchantAddress,
        instNo:'60000001',
        channelId: sessionStorage.getItem("channel")
    }
    // 商户结算信息
    this.settlement = {
        merchantType: this.merchantinfo.merchantType,
        businessLicence: this.merchantinfo.businessLicence,
        businessLicenceExpire: this.merchantinfo.businessLicenceExpire,
        accountType: this.merchantinfo.accountType,
        settlementType: this.merchantinfo.settlementType,
        legalName: this.merchantinfo.legalName,
        legalCertNo:this.merchantinfo.legalCertNo,
        legaCertExpire: this.merchantinfo.legaCertExpire,
        principalCertNo:this.merchantinfo.principalCertNo,
        principalCertExpire: this.merchantinfo.principalCertExpire,
        accountCardName: this.merchantinfo.accountCardName,
        accountCardNum: this.merchantinfo.accountCardNum,
        accountPhone: this.merchantinfo.accountPhone,
        parentBankNo: this.merchantinfo.parentBankNo,
        provinces: this.merchantinfo.accountBankProvince,
        accountBankProvince: this.merchantinfo.accountBankProvince,
        acccity: this.merchantinfo.accountBankCity,
        accountBankCity:this.merchantinfo.accountBankCity,
        accountBankName: this.merchantinfo.accountBankName,
        accountBankNo: this.merchantinfo.accountBankNo,
        shengNo: this.merchantinfo.accountBankNo.substring(0, 3),
        licenceType: this.merchantinfo.licenceType,
        photoType: this.merchantinfo.licenceType

    }
    // 小微
    this.settlement1 = {
        merchantType: this.merchantinfo.merchantType,
        businessLicence: this.merchantinfo.businessLicence,
        businessLicenceExpire: this.merchantinfo.businessLicenceExpire,
        accountType: this.merchantinfo.accountType,
        settlementType: this.merchantinfo.settlementType,
        legalName: this.merchantinfo.legalName,
        legalCertNo:this.merchantinfo.legalCertNo,
        principalCertNo:this.merchantinfo.principalCertNo,
        principalCertExpire: this.merchantinfo.principalCertExpire,
        accountCardName: this.merchantinfo.accountCardName,
        accountCardNum: this.merchantinfo.accountCardNum,
        accountPhone: this.merchantinfo.accountPhone,
        parentBankNo: this.merchantinfo.parentBankNo,
        provinces: this.merchantinfo.accountBankProvince,
        accountBankProvince: this.merchantinfo.accountBankProvince,
        acccity: this.merchantinfo.accountBankCity,
        accountBankCity:this.merchantinfo.accountBankCity,
        accountBankName: this.merchantinfo.accountBankName,
        accountBankNo: this.merchantinfo.accountBankNo,
        shengNo: this.merchantinfo.accountBankNo.substring(0, 3),
    }
    // 商戶费率及证照
    this.picture = {
        rateCode: this.merchantinfo.rateCode,
    }
    if(this.settlement.merchantType == 2){
        this.geti = true
        this.settlement.licenceType = 1
    }
    // let _this = this
    //     bus.$on('tags', msg => {
    //     console.log(msg)
    //     _this.tags = msg
    // })
  },

//   mounted(){
//       let _this = this
//      bus.$on('tags', msg => {
//        console.log(msg)
//        _this.tags = msg
//     })
//   },
  methods: {
    //   获取照片
    uploadfileget(){
        // console.log(this.merchantinfo.uploadId)
        uploadfileget({},this.merchantinfo.uploadId).then(res => {
            // console.log(res);
            if (res.code == 405) {
                sessionStorage.removeItem("token");
                this.$router.push("/login");
                } else if (res.code == 200) {
                    this.uploadfile = res.data
                    this.picture = {
                        pBankcardA: res.data.pBankcardA,
                        pBankcardB: res.data.pBankcardB,
                        pIdcardA: res.data.pIdcardA,
                        pIdcardB: res.data.pIdcardB,
                        pOutside: res.data.pOutside,
                        pInside: res.data.pInside,
                        pIdcardHander: res.data.pIdcardHander,
                        pBankIdcardA: res.data.pBankIdcardA,
                        pBankIdcardB: res.data.pBankIdcardB,
                        pOther: res.data.pOther,
                        pLicense: res.data.pLicense,
                        pOpenAccount: res.data.pOpenAccount,
                        pOrganization: res.data.pOrganization,
                        pTaxRegistration: res.data.pTaxRegistration,
                        pUnincorporated: res.data.pUnincorporated,
                        aliMaterials: res.data.aliMaterials,
                        otherPlatform: res.data.otherPlatform,
                        rateCode: this.merchantinfo.rateCode,
                    }
                }else{
                    this.$message.error(res.message);
                }
            
        });
    },
    // 商户类型
    merchantsType(val){
    //   console.log(val)
      if(val == 3){
        this.geti = false;
        this.settlement.licenceType = ''
        this.settlement1.merchantType = 3
        this.settlement1.accountType = 2
      }else if(val == 1){
          this.geti = false
        //   this.settlement.licenceType = 0
      }else if(val == 2){
           this.geti = true
          this.settlement.licenceType = 1
      }
    },
    // 获取一级服务商
    getservice(){
        getservice({
            type: 1
        }).then(res => {
            // console.log(res);
            for (var i = 0; i < res.data.rows.length; i++) {
            this.servicearr1.push({
                value: res.data.rows[i].agentname,
                id: res.data.rows[i].id
            });
            }
        });
    },
     // 一级服务商
    querySearch(queryString, cb) {
      let servicearr = this.servicearr1;
      let results = queryString
        ? servicearr.filter(this.createFilter(queryString))
        : servicearr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
     // 二级服务商
    querySearch2(queryString, cb) {
      let servicearr = this.servicearr2;
      let results = queryString
        ? servicearr.filter(this.createFilter(queryString))
        : servicearr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    // 银行支行
     querySearch3(queryString, cb) {
      let servicearr = this.bankarr;
      let results = queryString
        ? servicearr.filter(this.createFilter(queryString))
        : servicearr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    // 根据一级查2级
    serviceid(item){
        this.ruleForm.agent1Id = item.id
        this.servicearr2 =  []
         getservice({
            type: 2,
            parentid:item.id
        }).then(res => {
            // console.log(res);
            for (var i = 0; i < res.data.rows.length; i++) {
            this.servicearr2.push({
                value: res.data.rows[i].agentname,
                id: res.data.rows[i].id
            });
            }
        });
        // this.ruleForm.agent1Id = item.id
    },
    // 获取2级
     serviceid2(item){
         this.ruleForm.agent2Id = item.id
    //    console.log(item)
        this.getBD(item.id)
    },
     // 区银行支行信息
    serviceid3(item){
        // console.log(item)
        // this.settlement.accountBankName = item.bankName
        this.settlement.accountBankNo = item.id
        // this.settlement.accountBankName = item.bankName
        // this.settlement1.accountBankName = item.bankName
        // this.settlement1.accountBankName = item.bankName
        this.settlement1.accountBankNo = item.id
    },
     // 业态
    BDname(queryString, cb) {
      let BDarr = this.BDarr;
      let results = queryString
        ? BDarr.filter(this.createFilter(queryString))
        : BDarr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    // 获取BD
    getBD(id) {
      this.BDarr = [];
      getBD({ agentid: id }).then(res => {
        // console.log(res);
        for (var i = 0; i < res.data.rows.length; i++) {
          this.BDarr.push({
            value: res.data.rows[i].name,
            id: res.data.rows[i].id
          });
        }
      });
    },
    BDid(item) {
        this.ruleForm.bdId = item.id
         // 获取费率
        ratelist({
            bdId: item.id
        }).then(res => {
                if (res.code == 405) {
                sessionStorage.removeItem("token");
                this.$router.push("/login");
                } else {
                this.ratelists = res.data;
                }
        });
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !==
          -1
        );
      };
    },
    // 获取一级银行
    parentlist(){
        parentlist().then(res => {
            this.parentlists = res.data;
        });
    },
    parent(val){
         let obj = {};
        obj = this.parentlists.find(item => {
            //这里的userList就是上面遍历的数据源
            return item.parentBankNo === val; //筛选出匹配数据
        });
        // console.log(obj)
        this.settlement.shengNo = obj.parentBankNo
        this.settlement.accountBankName = ''
        this.settlement1.accountBankName = ''
        banklist({
            parentBankNo: obj.parentBankNo,
            cityId: this.cityId
        }).then(res => {
            // this.bankarr = res.data;
            this.bankarr = []
            for (var i = 0; i < res.data.length; i++) {
                this.bankarr.push({
                    value: res.data[i].bankName,
                    id: res.data[i].unionBankNo
                });
                }
        });
    },
      // 获取行业
    wx1(val) {
      wx1().then(res => {
        this.wx1arr = res.data;
      });
    },
    // 获取行业二级
    wx2fun(val) {
        // console.log(val)
        this.ruleForm.merchantMcc1 = val
      this.wx2arr = "";
      wx2({
        wx1Id: val
      }).then(res => {
        // console.log(res);
        this.wx2arr = res.data;
      });
    },
    // 获取行业三级
    wx3fun(val) {
        this.ruleForm.merchantMcc2 = val
      this.wx3arr = "";
      wx3({
        wx2Id: val,
        type: 'wx3'
      }).then(res => {
        // console.log(res);
        this.wx3arr = res.data;
      });
    },
    // 选取行业三级
    wx4fun(val){
        let obj = {};
        obj = this.wx3arr.find(item => {
            //这里的userList就是上面遍历的数据源
            return item.code === val; //筛选出匹配数据
        });
        // console.log(obj)
        this.ruleForm.merchantMcc3 = obj.id
        this.ruleForm.merchantMccName = obj.thirdname
        this.ruleForm.merchantMccCode = obj.code
    },
     //自动获取省市区
    province() {
      province().then(res => {
        var that = this;
        // console.log(res);
        this.provincearr = res.data;
        // console.log(this.merchantinfo.accountBankProvince)
        if(this.merchantinfo.accountBankProvince){
            var shengid= this.provincearr.filter(function(item){
                return item.code == that.merchantinfo.accountBankProvince; 
            })
        //    console.log(shengid)
            //  this.ruleForm.merchantProvince = shengid[0].name;
            //  this.ruleForm.provinceCode = shengid[0].code;
            city({
                provinceId: shengid[0].id
            }).then(res => {
                // console.log(res);
                this.citysarr = res.data;
                    console.log(this.merchantinfo.accountBankCity)
                     if(this.merchantinfo.accountBankCity){
                    var quid= res.data.filter(function(item){
                        console.log(item.code)
                        return item.code == that.merchantinfo.accountBankCity; 
                    })
                    // console.log(quid)
                    this.cityId = quid[0].id
                    //  banklist({
                    //     parentBankNo: this.settlement.shengNo,
                    //     cityId: quid[0].id
                    // }).then(res => {
                    //     // this.bankarr = res.data;
                    //     this.bankarr = []
                    //     for (var i = 0; i < res.data.length; i++) {
                    //         this.bankarr.push({
                    //             value: res.data[i].bankName,
                    //             id: res.data[i].unionBankNo
                    //         });
                    //         }
                    // });
                }
                   
                
            });
        }
         if(this.merchantinfo.provinceCode){
            var shengid= this.provincearr.filter(function(item){
                return item.code == that.merchantinfo.provinceCode; 
            })
        //    console.log(shengid)
            city({
                provinceId: shengid[0].id
            }).then(res => {
                // console.log(res);
                this.cityarr = res.data;
                if(this.merchantinfo.cityCode){
                    // console.log(res.data)
                    var quid= res.data.filter(function(items){
                        // console.log(items)
                        return items.code == that.merchantinfo.cityCode; 
                    })
                    // console.log(quid)
                    this.ruleForm.merchantCity = quid[0].name
                    this.ruleForm.cityCode = quid[0].code
                    county({
                        cityId: quid[0].id
                    }).then(res => {
                        // console.log(res);
                        this.countyarr = res.data;
                        if(this.merchantinfo.countyCode){
                            var qucode= res.data.filter(function(item){
                                return item.code == that.merchantinfo.countyCode; 
                            })
                            this.ruleForm.merchantCounty = qucode[0].name
                            this.ruleForm.countyCode = qucode[0].code
                        }
                    });
                }
            });
        }
        
        
      });
    },
    //获取市
    city(val) {
      let obj = {};
      obj = this.provincearr.find(item => {
        //这里的userList就是上面遍历的数据源
        return item.code === val; //筛选出匹配数据
      });
      this.ruleForm.merchantProvince = obj.name
      this.ruleForm.provinceCode = obj.code
      this.cityarr = "";
      this.ruleForm.city = "";
      this.ruleForm.cityCode = "";
      this.ruleForm.county = "";
      this.ruleForm.countyCode = "";
      city({
        provinceId: obj.id
      }).then(res => {
        // console.log(res);
        this.cityarr = res.data;
      });
    },
    //获取银行市code
    citys(val) {
      let obj = {};
      obj = this.provincearr.find(item => {
        //这里的userList就是上面遍历的数据源
        return item.code === val; //筛选出匹配数据
      });
    //   console.log(obj)
      this.settlement.accountBankProvince = obj.code
      this.settlement1.accountBankProvince = obj.code
      this.citysarr = "";
      this.settlement.acccity = '';
      this.settlement1.acccity = '';
      this.settlement.accountBankName = ''
        this.settlement1.accountBankName = ''
      city({
        provinceId: obj.id
      }).then(res => {
        // console.log(res);
        this.citysarr = res.data;
      });

    },
     // 获取银行区code
    countys(val){
        let obj = {};
        obj = this.citysarr.find(item => {
        //这里的userList就是上面遍历的数据源
        return item.code === val; //筛选出匹配数据
        });
        console.log(obj)
        this.settlement.accountBankCity = obj.code
        this.settlement1.accountBankCity = obj.code
        this.settlement.bankid = obj.id
        this.countysarr = "";
        this.settlement.accountBankName = ''
        this.settlement1.accountBankName = ''
        this.cityId = obj.id
      banklist({
        parentBankNo: this.settlement.shengNo,
        cityId:this.settlement.bankid
      }).then(res => {
        // this.bankarr = res.data;
        this.bankarr = []
        for (var i = 0; i < res.data.length; i++) {
            this.bankarr.push({
                value: res.data[i].bankName,
                id: res.data[i].unionBankNo
            });
            }
      });
    },
    // 选择开户支行
    accountBank(val){
         let obj = {};
        obj = this.bankarr.find(item => {
        //这里的userList就是上面遍历的数据源
        return item.unionBankNo === val; //筛选出匹配数据
        });
        // console.log(obj)
        this.settlement.accountBankName = obj.bankName
        this.settlement.accountBankNo = obj.unionBankNo
    },
    // 获取区
    county(val){
        let obj = {};
        obj = this.cityarr.find(item => {
        //这里的userList就是上面遍历的数据源
        return item.code === val; //筛选出匹配数据
        });
        this.ruleForm.merchantCity = obj.name
        this.ruleForm.cityCode = obj.code
        this.countyarr = "";
      county({
        cityId: obj.id
      }).then(res => {
        // console.log(res);
        this.countyarr = res.data;
      });
    },
    countyCode(val){
         let obj = {};
        obj = this.countyarr.find(item => {
        //这里的userList就是上面遍历的数据源
        return item.code === val; //筛选出匹配数据
        });
        // console.log(obj)
        this.ruleForm.merchantCounty = obj.name
        this.ruleForm.countyCode = obj.code
    },
    // 上传图片
    pBankcardA(res, file) { //银行卡正面
      if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pBankcardA = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
     
    },
    pBankcardB(res, file) { //银行卡反面
     if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pBankcardB = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pIdcardA(res, file){ //身份证正面
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pIdcardA = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pIdcardB(res, file){ //身份证正面
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pIdcardB = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pOutside(res, file){ // 外部门头照
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pOutside = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pInside(res, file){ // 内部环境照
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pInside = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pIdcardHander(res, file){ // 手持身份证照
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pIdcardHander = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pBankIdcardA(res, file){ // 对私结算人身份证正面照
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pBankIdcardA = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pBankIdcardB(res, file){ // 对私结算人身份证反面照
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pBankIdcardB = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
     pOther(res, file){ // 其他照片
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pOther = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
     pLicense(res, file){ // 营业执照
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pLicense = res.data.filePath;
         
        
      }else{
          this.$message.error(res.message);
      }
    },
     pOpenAccount(res, file){ // 开户许可证照片
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pOpenAccount = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
     pUnincorporated(res, file){ // 入账非法人证明
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pUnincorporated = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pOrganization(res, file){ // 组织机构代码
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pOrganization = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    pTaxRegistration(res, file){ // 税务登记照
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.picture.pTaxRegistration = res.data.filePath;
      }else{
          this.$message.error(res.message);
      }
    },
    otherPlatform(res, file){ // 第三方平台截图（绿洲）
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
          this.$set(this.picture,'otherPlatform',res.data.filePath)
      }else{
          this.$message.error(res.message);
      }
    },
    aliMaterials(res, file){ // 支付宝支付物料照片
        if(res.code ==405){
            sessionStorage.removeItem("token");
            this.$router.push("/login");
      }else if (res.code == 200) {
          this.$message({
            message: "上传成功",
            type: "success"
          });
        this.$set(this.picture,'aliMaterials',res.data.filePath)
      }else{
          this.$message.error(res.message);
      }
    },
    licenceType(val){
        this.settlement.photoType = val
    },
    
    // 修改商户
    updateToReview(merchaninfo){
         const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        var that = this
         updateToReview(merchaninfo,this.merchantinfo.id).then(res => {
        // console.log(res);
        if (res.code == 405) {
            loading.close();
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if(res.code == 200) {
            loading.close();
            this.$message({
                message: "修改成功",
                type: "success"
            });
            this.ruleForm =  {
                    agent1Id: '', // 一级服务商
                    agent2Id: '', //二级服务商
                    bdId: '', // DB
                    merchant_name: '',//商户名称
                    instNo:'60000001'
                }
            this.settlement = {
                                licenceType:'',
                                photoType: '1'
                            },
            this.picture = {
                            pBankcardA:'',
                            pBankcardB:'',
                            pIdcardA:'',
                            pIdcardB:'',
                            pOutside:'',
                            pInside:'',
                            pIdcardHander:'',
                            pBankIdcardA:'',
                            pBankIdcardB:'',
                            pOther:'',
                            pLicense:'',
                            pOpenAccount:'',
                            pUnincorporated:''
                        },
            this.settlement1 ={}
            // this.closetab()
             this.$router.push({
                            path: "/mymerchants",
                        });
            setTimeout(function(){
                that.$router.go(0);
            },300)
        }else{
            this.$message.error(res.message);
            loading.close();
        }
      });
    },
    // 修改商户(渠道)
    merchantupdate(merchaninfo){
        // console.log(merchaninfo)
         const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        var that = this
        merchantupdate(merchaninfo,this.merchantinfo.id).then(res => {
        // console.log(res);
        if (res.code == 405) {
            loading.close();
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if(res.code == 200) {
            loading.close();
            this.$message({
                message: "修改成功",
                type: "success"
            });
            this.ruleForm =  {
                    agent1Id: '', // 一级服务商
                    agent2Id: '', //二级服务商
                    bdId: '', // DB
                    merchant_name: '',//商户名称
                    instNo:'60000001'
                }
            this.settlement = {
                                licenceType:'',
                                photoType: '1'
                            },
            this.picture = {
                            pBankcardA:'',
                            pBankcardB:'',
                            pIdcardA:'',
                            pIdcardB:'',
                            pOutside:'',
                            pInside:'',
                            pIdcardHander:'',
                            pBankIdcardA:'',
                            pBankIdcardB:'',
                            pOther:'',
                            pLicense:'',
                            pOpenAccount:'',
                            pUnincorporated:''
                        },
            this.settlement1 ={}
            // this.closetab()
             this.$router.push({
                            path: "/mymerchants",
                        });
            setTimeout(function(){
                that.$router.go(0);
            },300)
        }else{
            loading.close();
            this.$message.error(res.message);
        }
      });
    },
    // 修改商户
    addmerchant(){
        let channel = sessionStorage.getItem("channel")
        let allinfo;
        console.log(this.settlement.merchantType)
        if(this.settlement.merchantType == 1 || this.settlement.merchantType ==2){
            allinfo = Object.assign(this.ruleForm,this.settlement,this.picture)  //企业
            }else if(this.settlement.merchantType == 3){
                allinfo = Object.assign(this.ruleForm,this.settlement1,this.picture)  //小微
            }
        if(channel){
            // console.log(allinfo)
            this.merchantupdate(allinfo)
        }else{
            // console.log(allinfo)
            this.updateToReview(allinfo)
        }
       
    },
    // 关闭当前页签
    closetab(){
        let that = this;
        // console.log(that.tags)
        let _this = this
        for(var i=0;i<this.tags.length;i++){
            // console.log(this.tags[i].title)
          if(this.tags[i].title == '修改商户'){
             this.tags.splice(i, 1)[0]
            bus.$emit('tags', _this.tags);
            this.$router.push('/mymerchants');
          }
        }
    }
  },
};
</script>


<style scoped lang="less">
.err{
    color: red;
    font-size: 20px;
    width: 70%;
}
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.Box {
  width: 100%;
  background: #fff;
  padding: 10px;
}
.basicBox{
    width: 70%;
    text-align: center;
    margin: 0 auto;
}
.basicBoxs{
     width: 75%;
    text-align: center;
    margin: 0 auto;
}
.width100{
    width: 100%;
}
.block{
    text-align: left;
}

.photo{
    width: 25%;
    display: inline-block;
}
.btnBox{
    width: 100%;
    text-align: center;
}
.service{
    width: 100%;
}
</style>
